package com.sdk.paas.core;

import com.alibaba.fastjson.JSONObject;
import com.sdk.paas.core.enums.CommonResultCode;
import lombok.Data;

import java.io.Serializable;

@Data
public class Result<T> implements Serializable {
    private static final long serialVersionUID = -5478036697217775660L;

    private T result;
    private String code;
    private String msg;

    public Result() {
    }

    public static <T> Result<T> success() {
        Result<T> result = new Result();
        result.setCode(CommonResultCode.SUCCESS.getResultCode());
        result.setMsg(CommonResultCode.SUCCESS.getResultMsg());
        result.setResult(null);
        return result;
    }

    public static <T> Result<T> success(T data) {
        Result<T> result = new Result();
        result.setCode(CommonResultCode.SUCCESS.getResultCode());
        result.setMsg(CommonResultCode.SUCCESS.getResultMsg());
        result.setResult(data);
        return result;
    }

    public static <T> Result<T> failed(String code, String msg) {
        Result<T> result = new Result();
        result.setCode(code);
        result.setMsg(msg);
        result.setResult(null);
        return result;
    }

    public static <T> Result<T> failed(String msg) {
        Result<T> result = new Result();
        result.setCode(CommonResultCode.FAILED.getResultCode());
        result.setMsg(msg);
        result.setResult(null);
        return result;
    }

    public static <T> Result<T> failed(T data) {
        Result<T> result = new Result();
        result.setCode(CommonResultCode.FAILED.getResultCode());
        //result.setMsg(msg);
        result.setResult(data);
        return result;
    }

    public boolean isOk() {
        return CommonResultCode.SUCCESS.getResultCode().equals(this.code);
    }

    public String toString() {
        return JSONObject.toJSONString(this);
    }

    public T getResult() {
        return this.result;
    }

    public String getCode() {
        return this.code;
    }

    public String getMsg() {
        return this.msg;
    }

    public Result<T> setResult(final T result) {
        this.result = result;
        return this;
    }

    public Result<T> setCode(final String code) {
        this.code = code;
        return this;
    }

    public Result<T> setMsg(final String msg) {
        this.msg = msg;
        return this;
    }

    public static <T> Result<T> result(String code, String msg, T data) {
        Result<T> res = new Result();
        res.setCode(code);
        res.setMsg(msg);
        res.setResult(data);
        return res;
    }
}
